var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()
var Store = app.Store

Page({
  data: {
    form:{}, // redux传过来的参数
    steps: [
    ],
    active:0,
    isLose: false,  
    rejectReason:'',
    urls:[],
    signImg:'',
    type:''
  },
  onLoad: function (options) {
    this.setData({
      type:options.type,
      ticketNumber:options.ticketNumber
    })
    api.$get(api.operateTicket.operateTickInfo+options.ticketNumber).then((res) => {
      this.setData({
        ticketNumber:options.ticketNumber,
        urls:[res.data.imagePath+`?a=${Math.random()*100}`],
        steps:res.data.operationTasksList.map((item,index)=>{
          return {
            text: '步骤'+(index+1),
            desc: item.taskDescription, 
          }
        })
      })
      wx.previewImage({
        current: '', // 当前显示图片的链接，不填则默认为urls的第一张
        urls: this.data.urls // 需要预览的图片链接列表
      })
    })
  },
  onShow: function () {
    var form = Store.getState().formInfor.form
    var key = form.key
    console.log(form)
    switch (key) {
      case 'operateSign': // 获取签发人签名  
        this.setData({
          signImg: form.selected,
        })
        break 
    
      default:
        this.setData({
          signImg: wx.getStorageSync('signUrl'),
        })
        break;
    }
    console.log(this.data.signImg)
  },
showTickets(e) {
  const {type,item,idx}=$.detail(e)
  wx.previewImage({
    current: '', // 当前显示图片的链接，不填则默认为urls的第一张
    urls: this.data.urls // 需要预览的图片链接列表
  })
  
},  
nextProcess(){
  this.setData({active:this.data.active+1})
},
beginSign(e){  
  var that=this
  if(!Boolean(that.data.signImg)){
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'operateSign', 
      },
    })
    $.linkurl(`/pages/components/signatrue/signAture?ticketNumber=${that.data.ticketNumber}`) 
  }
  // wx.showModal({
  //   title: '提示',
  //   content: '使用原有签名或者手动签名',
  //   confirmText:'原有签名',
  //   cancelText:'手动签名',
  //   success (res) {
  //     if (res.confirm) {
        
  //       that.setData({
  //         signImg: wx.getStorageSync('signUrl'),
  //       })
  //     } else if (res.cancel) {
  //       Store.dispatch({
  //         type: 'SET_FROM',
  //         form: {
  //           key: 'operateSign', 
  //         },
  //       })
  //       $.linkurl(`/pages/components/signatrue/signAture?ticketNumber=${that.data.ticketNumber}`) 
  //     }
  //   }
  // })
},
bigSign(e){
  const {img}=$.detail(e)
  wx.previewImage({
    current: '', // 当前显示图片的链接，不填则默认为urls的第一张
    urls: [img] // 需要预览的图片链接列表
  })
},
ticketConfirm(approvalResult,approvalRemark){
  if(!Boolean(this.data.signImg)){
    return $.toast('当前无默认签名，请手动填写！')
  }
  api.$get(api.operateTicket.ticketConfirm,{
    ticketNumber:this.data.ticketNumber,
    signature:this.data.signImg,
    approvalResult:approvalResult,
    approvalRemark:approvalRemark
  }).then((res) => {
    if(res.code==200){
      $.linkurl(``,'back')
    }
  })
},
ticketReceiptOrder(){
  api.$get(api.operateTicket.ticketReceiptOrder,{
    ticketNumber:this.data.ticketNumber,
  }).then((res) => {
    if(res.code==200){
      $.linkurl(``,'back')
    }
  })
},
czrRiskCardConfirm(approvalResult,approvalRemark){
  if(!Boolean(this.data.signImg)){
    return $.toast('当前无默认签名，请手动填写！')
  }
  api.$get(api.operateTicket.czrRiskCardConfirm,{
    ticketNumber:this.data.ticketNumber,
    signature:this.data.signImg,
    approvalResult:approvalResult,
    approvalRemark:approvalRemark
  }).then((res) => {
    if(res.code==200){
      $.linkurl(``,'back')
    }
  })
},
jhrRiskCardConfirm(approvalResult,approvalRemark){
  if(!Boolean(this.data.signImg)){
    return $.toast('当前无默认签名，请手动填写！')
  }
  api.$get(api.operateTicket.jhrRiskCardConfirm,{
    ticketNumber:this.data.ticketNumber,
    signature:this.data.signImg,
    approvalResult:approvalResult,
    approvalRemark:approvalRemark
  }).then((res) => {
    if(res.code==200){
      $.linkurl(``,'back')
    }
  })
},
zbrRiskCardConfirm(approvalResult,approvalRemark){
  if(!Boolean(this.data.signImg)){
    return $.toast('当前无默认签名，请手动填写！')
  }
  api.$get(api.operateTicket.zbrRiskCardConfirm,{
    ticketNumber:this.data.ticketNumber,
    signature:this.data.signImg,
    approvalResult:approvalResult,
    approvalRemark:approvalRemark
  }).then((res) => {
    if(res.code==200){
      $.linkurl(``,'back')
    }
  })
},

setReject(){//驳回
  if($.isNull(this.data.rejectReason)){
    $.toast("驳回原因不能为空")
      return
  }
  if(this.data.type=='发令人发令')this.ticketConfirm(false,this.data.rejectReason)//发令人发令驳回
  if(this.data.type=='操作人审核')this.czrRiskCardConfirm(false,this.data.rejectReason)//操作人审核驳回
  if(this.data.type=='监护人审核')this.jhrRiskCardConfirm(false,this.data.rejectReason)//监护人审核驳回
  if(this.data.type=='值班人审核')this.zbrRiskCardConfirm(false,this.data.rejectReason)//值班人审核驳回

},
openLoseHandle() { // 驳回弹窗
  if(this.data.key=='shouling'){
    $.linkurl('','back')
  }else{
    this.setData({ isLose: true })
  }
  
},
updateHandle(){
  // $.linkurl('/pages/operationTicket/beginOperate/beginOperate')
  if(this.data.type=='发令人发令')this.ticketConfirm(true,'')//发令人发令
  if(this.data.type=='受令人收令')this.ticketReceiptOrder()//受令人受令
  if(this.data.type=='操作人审核')this.czrRiskCardConfirm(true,'')//操作人审核
  if(this.data.type=='监护人审核')this.jhrRiskCardConfirm(true,'')//监护人审核
  if(this.data.type=='值班人审核')this.zbrRiskCardConfirm(true,'')//值班人审核
},
input_handle(e){
  this.setData({
    [e.currentTarget.dataset.key]: e.detail.value
  })
}


})